let AC_GAME_OBJECTS = []

export class AcGameObject {

    constructor() {
        AC_GAME_OBJECTS.push(this)

        this.timedelta = 0
        this.has_call_start = false
    }

    start() { // 初始执行一次

    }

    update() { // 每一帧都执行一次(除了第一帧以外)

    }

    destroy() {
        for (let i = 0; i < AC_GAME_OBJECTS.length; i++) {
            if (AC_GAME_OBJECTS[i] === this) {
                AC_GAME_OBJECTS.splice(i, 1)
                break
            }
        }
    }
}

let last_timestamp;

let AC_GAME_OBJECTS_FRAME = (timestamp) => { // 每一帧执行一次
    for (let obj of AC_GAME_OBJECTS) { // for-in是枚举下标; for-of是枚举值
        if (!obj.has_call_start) {
            obj.start()
            obj.has_call_start = true
        } else {
            obj.timedelta = timestamp - last_timestamp
            obj.update()
        }
    }

    last_timestamp = timestamp
    requestAnimationFrame(AC_GAME_OBJECTS_FRAME)
}

requestAnimationFrame(AC_GAME_OBJECTS_FRAME)